package com.bw.lib_core.mvvm.viewmodel;

import android.os.Bundle;

import androidx.lifecycle.ViewModel;


import com.bw.lib_core.bean.BaseBean;
import com.bw.lib_core.bean.ErrorBean;
import com.bw.lib_core.bean.UIBusBean;
import com.bw.lib_core.bus.ActivityAction;
import com.bw.lib_core.bus.ActivityParams;
import com.bw.lib_core.bus.BusLiveData;
import com.bw.lib_core.field.ErrorCode;
import com.bw.lib_core.mvvm.model.BaseModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseViewModel<M extends BaseModel> extends ViewModel implements IViewModel, Observer<BaseBean> {

    //持有Model对象
    protected M mModel;
    //收集全部RxJava订阅，防止内存泄漏
    private CompositeDisposable disposable;
    //LiveData响应UI层业务
    private BusLiveData<UIBusBean> liveData;

    public BusLiveData<UIBusBean> getLiveData() {
        if (liveData == null) {
            liveData = new BusLiveData<>();
        }
        return liveData;
    }

    @Override
    public void create() {
        disposable = new CompositeDisposable();
        createModel();
    }

    @Override
    public void destroy() {
        disposable.dispose();
        disposable.clear();
    }


    /**
     * Model的反射创建
     */
    private void createModel() {
        Type type = getClass().getGenericSuperclass();
        Class modelClass;
        if (type instanceof ParameterizedType) {
            modelClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
        } else {
            modelClass = BaseModel.class;
        }
        try {
            mModel = (M) modelClass.newInstance();
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSubscribe(Disposable d) {
        disposable.add(d);
        //开启Loading
        showLoading();
    }

    @Override
    public void onNext(BaseBean baseBean) {
        if (baseBean.status != 200) {
            //通信成功错误
        }
        onResult(baseBean);
    }

    @Override
    public void onError(Throwable e) {
        //网络错误
        ErrorBean bean = new ErrorBean();
        bean.status = ErrorCode.NETWORK_ERROR;
        bean.message = e.getMessage();
        showError(bean);
    }

    @Override
    public void onComplete() {
        //关闭Loading
        hideLoading();
    }

    protected abstract void onResult(BaseBean baseBean);

    protected void showLoading() {
        getLiveData().setValue(new UIBusBean(ActivityAction.SHOW_LOADING));
    }

    protected void hideLoading() {
        getLiveData().setValue(new UIBusBean(ActivityAction.HIDE_LOADING));
    }

    protected void showError(ErrorBean errorBean) {
        UIBusBean uiBusBean = new UIBusBean(ActivityAction.SHOW_ERROR);
        Map<String, Object> data = new HashMap<>();
        data.put(ActivityParams.ERROR_KEY, errorBean);
        uiBusBean.data = data;
        getLiveData().setValue(uiBusBean);
    }

    protected void hideError() {
        getLiveData().setValue(new UIBusBean(ActivityAction.HIDE_ERROR));
    }

    protected void startPage(Class<?> clazz) {
        UIBusBean uiBusBean = new UIBusBean(ActivityAction.START_PAGE);
        Map<String, Object> data = new HashMap<>();
        data.put(ActivityParams.CLASS_KEY, clazz);
        uiBusBean.data = data;
        getLiveData().setValue(uiBusBean);
    }

    protected void startPage(Class<?> clazz, Bundle bundle) {
        UIBusBean uiBusBean = new UIBusBean(ActivityAction.START_PAGE_BUNDLE);
        Map<String, Object> data = new HashMap<>();
        data.put(ActivityParams.CLASS_KEY, clazz);
        data.put(ActivityParams.BUNDLE_KEY, bundle);
        uiBusBean.data = data;
        getLiveData().setValue(uiBusBean);
    }

    protected void finishPage() {
        getLiveData().setValue(new UIBusBean(ActivityAction.FINISH_PAGE));

    }


}
